package cn.tmpan.controller.user;

import cn.tmpan.common.ServerResponse;
import cn.tmpan.dao.MemberMapper;
import cn.tmpan.entity.Member;
import cn.tmpan.enums.ResponseCode;
import cn.tmpan.service.INotifyService;
import cn.tmpan.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by erhuo on 2018/3/10.
 */
@Controller
@RequestMapping("/user/")
public class UserController {


    private Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private IUserService iUserService;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private INotifyService notifyService;

    /**
     * @param uid
     * @return
     */
    @RequestMapping(value = "getUserInfo/{uid}", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse getUserInfo(@PathVariable Integer uid) {
        return iUserService.getUserInfo(uid);
    }


    /**
     * 用户登陆
     * @param member
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Member> login(Member member) {
        return iUserService.login(member);
    }

    /**
     * 用户注册
     * @param member
     * @return
     */
    @RequestMapping(value = "reg", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Member> reg(Member member) {
        String password = member.getPassword();
        ServerResponse ret =  iUserService.reg(member);
        if(ret.getStatus()== ResponseCode.SUCCESS.getCode()){
            member.setPassword(password);
            return  iUserService.login(member);
        }
        return  ret;
    }

    /**
     * 发送验证码
     * @param request
     * @return
     */
    @RequestMapping("sendcheckcode")
    @ResponseBody
    public ServerResponse sendcheckcode(HttpServletRequest request) {
        String phone = request.getParameter("phone");
        String sid = request.getParameter("sid");
        Member usr = null;
        usr = memberMapper.selectUserBySid(sid);

        if(usr==null||sid==null){
            return ServerResponse.createByErrorMessage("请先登录");
        }

        ServerResponse ret = notifyService.SendSmsCheckCode(phone,usr.getId());
        return  ret;
    }


    /**
     * 绑定手机号
     * @param request
     * @return
     */
    @RequestMapping(value = "bindphone", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse bindphone(HttpServletRequest request) {
        String phone = request.getParameter("phone");
        String sid = request.getParameter("sid");
        String code = request.getParameter("code");

        Member usr = null;
        usr = memberMapper.selectUserBySid(sid);
        if(usr==null||sid==null){
            return ServerResponse.createByErrorMessage("请先登录");
        }
        return  iUserService.bindPhone(phone,code,usr.getId());
    }
}
